package HtServer;

import HtServer.GrpcDataSet.DataSetImpl;
import HtServer.grpc.MeetingGreeter;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.annotation.WebListener;

import jakarta.servlet.ServletContextListener;
import java.io.IOException;

//配置grpc 的服务侦听伴随tomcat 的servlet 启动
//启动的端口50088
@WebListener
public class HtGrpcServer implements ServletContextListener {
    private Server server;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            System.out.println("HtGrpcServer");
            server = ServerBuilder.forPort(50088)
                    .addService(new GreeterImpl())
                    .addService(new MeetingGreeter())
                    .addService(new DataSetImpl())
                    .build()
                    .start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed");
        if (server != null) {
            server.shutdown();
        }
    }
}
